#include "listviewdelegate.h"
#include <QFileSystemModel>

ListViewDelegate::ListViewDelegate()
{
    // TODO Auto-generated constructor stub
    map=new QMap<qint64, int>;
}

ListViewDelegate::~ListViewDelegate()
{
    // TODO Auto-generated destructor stub

}

//alocate each item size in listview.
QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                                 const QModelIndex & index) const
{
    return QStyledItemDelegate::sizeHint(option,index);
    /*
    QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    QSize iconsize = icon.actualSize(option.decorationSize);
    QFont font = QApplication::font();
    QFontMetrics fm(font);

    return(QSize(iconsize.width(),iconsize.height()+fm.height() +8 ));*/

}
void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
{
    QStyledItemDelegate::paint(painter,option,index);

    painter->save();

    //painter->drawRect(option.rect);
    QIcon icon;

    //if (QFileSystemModel::fileInfo(index).absolutePath().contains(basePath)){
        //icon=QIcon(":/icons16/clear");
        if (map->contains(index.internalId())){
            switch (map->value(index.internalId())){
            case FileModified: icon=QIcon(":/icons16/important");break;
            case FileUnknown: icon=QIcon(":/icons16/unknown");break;
            case FileAdded: icon=QIcon(":/icons16/add");break;
            case FileClear: icon=QIcon(":/icons16/clear");break;
            default: break;
            }
        }
        QSize iconsize = icon.actualSize(option.decorationSize);
        if (option.rect.width()<200 && option.rect.height()>=48 && index.column()==0)
            painter->drawPixmap(QPoint((option.rect.left()+option.rect.width()/2)-option.decorationSize.width()/2,option.rect.top()+option.decorationSize.height()-16),icon.pixmap(iconsize.width(),iconsize.height()));
        else if (index.column()==0)
            painter->drawPixmap(QPoint(option.rect.left()+5,option.rect.top()+option.decorationSize.height()-16),icon.pixmap(iconsize.width(),iconsize.height()));

    painter->restore();
}

void ListViewDelegate::setOverlayStatus(qint64 index,int status){
    /*if (status== FileClear)
        map->remove(index);
    else*/
    if(status == FileClear || FileModified || status == FileAdded || status == FileUnknown ){
        map->insert(index,status);
    }
}

void ListViewDelegate::removeOverlayStatus(qint64 index){
    map->remove(index);
}

int ListViewDelegate::testOverlayStatus(qint64 index){
    if (map->contains(index))
        return map->value(index);
    else
        return -1;
}

void ListViewDelegate::setBasePath(QString path){
    basePath=path;
}

void ListViewDelegate::setMap(QMap<qint64, int> *map){
    this->map=map;
}
